package com.yauthor.module
{
	import flexunit.framework.TestCase;
	
	import mx.effects.Resize;

	public class ResultTest extends TestCase{
		
		protected var m_state:ComponentState;
		
		public function testSaveState():void{
			var result:Result;
			var childResult:Result;
			
			//test simple result with includeErrorsInResult set to true
			result = saveAndLoadState(new Result(3, 1, 1));
			
			assertEquals("simple result, todo", result.todo, 3);
			assertEquals("simple result, done", result.done, 1);
			assertEquals("simple result, errors", result.errors, 1);
			assertEquals("simple result, errorsInResult", result.errorsInResult, 1);
			assertEquals("simple result, result", result.result, 0);
			assertTrue("simple result, includeErrorsInResult", result.includeErrorsInResult);
			assertFalse("simple result, evaluationPending", result.evaluationPending);
			
			//test simple result with includeErrorsInResult set to false
			result = saveAndLoadState(new Result(3, 1, 1, false));
			
			assertEquals("simple result, false, todo", result.todo, 3);
			assertEquals("simple result, false, done", result.done, 1);
			assertEquals("simple result, false, errors", result.errors, 1);
			assertEquals("simple result, false, errorsInResult", result.errorsInResult, 0);
			assertEquals("simple result, false, result", result.result, 33);
			assertFalse("simple result, false, includeErrorsInResult", result.includeErrorsInResult);
			assertFalse("simple result, false, evaluationPending", result.evaluationPending);	
			
			//test appended result
			result = new Result(4, 2, 1);
			result.append(new Result(3, 1, 1, false)); 	
			result = saveAndLoadState(result);
			
			assertEquals("appended result, false, todo", result.todo, 7);
			assertEquals("appended result, false, done", result.done, 3);
			assertEquals("appended result, false, errors", result.errors, 2);
			assertEquals("appended result, false, errorsInResult", result.errorsInResult, 1);
			assertEquals("appended result, false, result", result.result, 28);
			assertTrue("appended result, false, includeErrorsInResult", result.includeErrorsInResult);
			assertFalse("appended result, false, evaluationPending", result.evaluationPending);
		}
		
		public function testCountMode():void{
			var result:Result;
			var resultData:Array = [
				[[9,3,33], [9,3,33]],
				[[8,3,37], [8,3,38]]
			];
			
			
			for(var i:uint = 0; i < resultData.length; i++){
				var exampleData:Array = resultData[i];
				
				for(var j:uint = 0; j < exampleData.length; j++){
					var modeData:Array = exampleData[j];
					var todo:Number = modeData[0];
					var done:Number = modeData[1];
					var message:String = ", todo = " + todo + ", done = " + done + ", mode = "; 
					
					result = new Result(todo, done);
					result.initMappedResult(result, true);
					
					if(j == 0){
						message += "floor";
					}else{
						message += "round";
						result.resultCountMode = ResultCountMode.ROUND;
					}
					
					assertEquals("Result" + message, modeData[2], result.result);
					assertEquals("Result points" + message, modeData[2], result.resultPoints);
				}
			}
		}
		
		public function testAppend():void{
			var result:Result
			var toAppendResult:Result
			result = new Result(4, 2, 1);
			result.append(new Result(3, 1, 1, false)); 	
			assertEquals("appended result, false, todo", result.todo, 7);
			assertEquals("appended result, false, done", result.done, 3);
			assertEquals("appended result, false, errors", result.errors, 2);
			assertEquals("appended result, false, result", result.result, 28);
			assertEquals("appended result, false, errorsInResult", result.errorsInResult, 1);
			
			
			result = new Result(2,2,0);
			result.initMappedResult(new Result(1,1,0));
			
			assertEquals("result,todo", result.todo, 2);
			assertEquals("result,done", result.done, 2);
			assertEquals("result,errors", result.errors, 0);
			assertEquals("result,result", result.result, 100);
			
			assertEquals("result,todo points", result.todoPoints, 1);
			assertEquals("result,done points", result.donePoints, 1);
			assertEquals("result,errors points", result.errorsPoints, 0);
			assertEquals("result,result points", result.resultPoints, 100);
			
			toAppendResult = new Result(3,3,0);
			toAppendResult.initMappedResult(new Result(1,1,0));
				
			result.append(toAppendResult);	
			
			assertEquals("result,todo", result.todo, 5);
			assertEquals("result,done", result.done, 5);
			assertEquals("result,errors", result.errors, 0);
			assertEquals("result,result", result.result, 100);
			
			assertEquals("result,todo points", result.todoPoints, 2);
			assertEquals("result,done points", result.donePoints, 2);
			assertEquals("result,errors points", result.errorsPoints, 0);
			assertEquals("result,result points", result.resultPoints, 100);
			
			result = new Result(7,3,2);
			result.append(toAppendResult);
			
			assertEquals("result,todo", result.todo, 10);
			assertEquals("result,done", result.done, 6);
			assertEquals("result,errors", result.errors, 2);
			assertEquals("result,result", result.result, 40);
			
			assertEquals("result,todo points", result.todoPoints, 8);
			assertEquals("result,done points", result.donePoints, 4);
			assertEquals("result,errors points", result.errorsPoints, 2);
			assertEquals("result,result points", result.resultPoints, 25);
			
			result = new Result(7,3,2,false);
			toAppendResult = new Result(3,3,0);
			toAppendResult.initMappedResult(new Result(2,1,0));
			
			result.append(toAppendResult);
			
			assertEquals("result,todo", result.todo, 10);
			assertEquals("result,done", result.done, 6);
			assertEquals("result,errors", result.errors, 2);
			assertEquals("result,result", result.result, 60);
			
			assertEquals("result,todo points", result.todoPoints, 9);
			assertEquals("result,done points", result.donePoints, 4);
			assertEquals("result,errors points", result.errorsPoints, 2);
			assertEquals("result,result points", result.resultPoints, 44);
			
			result = new Result(1, 1);
			toAppendResult = new Result(3);
			toAppendResult.initMappedResult(new Result(1));
			result.append(toAppendResult);
			
			assertEquals("result,todo", 4, result.todo);
			assertEquals("result,done", 1, result.done);
			assertEquals("result,errors", 0, result.errors);
			assertEquals("result,result", 25, result.result);
			
			assertEquals("result,todo points", 2, result.todoPoints);
			assertEquals("result,done points", 0, result.errorsPoints);
			assertEquals("result,errors points", 1, result.donePoints);
			assertEquals("result,result points", 50, result.resultPoints);
			
			
			result = new Result(3);
			toAppendResult = new Result(1, 1);
			result.initMappedResult(new Result(1));
			result.append(toAppendResult);
			
			assertEquals("result,todo", 4, result.todo);
			assertEquals("result,done", 1, result.done);
			assertEquals("result,errors", 0, result.errors);
			assertEquals("result,result", 25, result.result);
			
			assertEquals("result,todo points", 2, result.todoPoints);
			assertEquals("result,done points", 0, result.errorsPoints);
			assertEquals("result,errors points", 1, result.donePoints);
			assertEquals("result,result points", 50, result.resultPoints);
			
		}
		
		public function equals():void{
			var res0:Result
			var res1:Result
			
			res0 = new Result(2,2,1);
			res1 = new Result(2,2,1)
			
			assertTrue("result equals", res0.equals(res1));
			
			res0 = new Result(2,2,1);
			res1 = new Result(2,2,1,false)
				
			assertFalse("result equals", res0.equals(res1));
			
			res0 = new Result(10,5,0);
			res0.initMappedResult(new Result(1,1,0));
			
			res1 = new Result(10,5,0);
			res1.initMappedResult(new Result(1,1,0));
			
			assertTrue("result equals", res0.equals(res1));
			
			
			res0 = new Result(10,5,0);
			res0.initMappedResult(new Result(1,1,0));
			
			res1 = new Result(10,5,0);
			
			assertFalse("result equals", res0.equals(res1));
			
			res1 = new Result(10,5,0);
			res1.initMappedResult(new Result(20,20,1));
			
			assertFalse("result equals", res0.equals(res1));
		}
		
		
		
		private function saveAndLoadState(res:Result):Result{
			saveState(res);
			return loadState();
		}
		
		private function loadState():Result{
			var result:Result = new Result();
			
			m_state.prepareStateLoad()
			m_state.loadChildState(result);
			
			return result;			
		}
		
		private function saveState(res:Result):void{
			m_state = new ComponentState();
			m_state.prepareStateSave();
			m_state.saveChildState(res);
		}
		
	}
}